<?php

namespace App\Http\Controllers\Agentadmin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;

class LoginController extends Controller
{
    //登陆
    public function login(Request $request)
    {
        if($request->isMethod('post')){
            $this->validate($request,[
                'name'=>'required',
                'password'=>'required'
            ],[
                'name.required'=>'账号不能为空',
                'password.required'=>'密码不能为空'
            ]);
            $userInfo =\DB::table('user')->where(['user_name'=>$request->name])->where(['status'=>1])->whereIn('type',[1,2])->first();
            if (!$userInfo){
                return back()->withErrors('该账号不存在');
            }
            if (!Hash::check($request->password,$userInfo->password)){
                return back()->withErrors('密码不正确');
            }
            session(['user_id'=>$userInfo->user_id]);
                return redirect('agentadmin/index');
        }else{
            return view('agentadmin.login');
        }
    }
    //退出登陆
    public function loginOut(Request $request)
    {
        $request->session()->flush();
        return redirect('agentadmin/login');
    }

}
